﻿using FarmSmartSleep.Common.Domain;
using FarmSmartSleep.Common.Domain.BeginTable;
using FarmSmartSleep.Common.Domain.BodyWeight;
using FarmSmartSleep.Common.Dtos;
using FarmSmartSleep.Common.Infrastructure;
using MediatR;

namespace FarmSmartSleep.Organ.API.Application.SleepWeightCommands
{
    public class BodyMeanSearchHandler : IRequestHandler<BodyMeanSearchCommand, ResultT<BodyMeanDto>>
    {
        private readonly IBaseRepository<BodyMeans> BodyMeansbaseRepository;
        private readonly IBaseRepository<SleepInfor> SleepInforbaseRepository;
        private readonly IBaseRepository<Variety> VarietybaseRepository;
        private readonly IBaseRepository<Cottages> cottagesbaseRepository;
        private readonly IBaseRepository<Fields> fieldsbaseRepository;
        private readonly IBaseRepository<CottageType> cottagestypebaseRepository;
        public BodyMeanSearchHandler(IBaseRepository<BodyMeans> bodyMeansbaseRepository, IBaseRepository<SleepInfor> SleepInforbaseRepository,
                                     IBaseRepository<Variety> VarietybaseRepository, IBaseRepository<Cottages> cottagesbaseRepository,
                                     IBaseRepository<Fields> fieldsbaseRepository, IBaseRepository<CottageType> cottagestypebaseRepository)
        {
            BodyMeansbaseRepository = bodyMeansbaseRepository;
            this.SleepInforbaseRepository = SleepInforbaseRepository;
            this.VarietybaseRepository = VarietybaseRepository;
            this.cottagesbaseRepository = cottagesbaseRepository;
            this.fieldsbaseRepository = fieldsbaseRepository;
            this.cottagestypebaseRepository = cottagestypebaseRepository;
        }

        /// <summary>
        /// 体尺测定查询
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<ResultT<BodyMeanDto>> Handle(BodyMeanSearchCommand request, CancellationToken cancellationToken)
        {
            var result=new ResultT<BodyMeanDto>() { Code=(int)CodeEnum.add};
            var list=(from a in BodyMeansbaseRepository.GetAll()
                     join b in SleepInforbaseRepository.GetAll() on a.EarNumber equals b.EarNumber
                     join c in VarietybaseRepository.GetAll() on b.VarietyId equals c.VarietyId
                     join d in cottagesbaseRepository.GetAll() on b.RecordcottageId equals d.CottagesId
                     join e in fieldsbaseRepository.GetAll() on b.FieIdId equals e.FieIdId
                     join f in cottagestypebaseRepository.GetAll() on d.CottageTypeId equals f.CottageTypeId
                     select new BodyMeanDto()
                     {
                         BodyMeanId = a.BodyMeanId,
                         BodyMeanDate = a.BodyMeanDate,
                         EarNumber = a.EarNumber,
                         BodyTall = a.BodyTall,
                         BodyLength = a.BodyLength,
                         BodyBust = a.BodyBust,
                         BodyPipe = a.BodyPipe,
                         BodyScrotal = a.BodyScrotal,
                         BodyBackfat = a.BodyBackfat,
                         EyeArea = a.EyeArea,
                         EyeWidth = a.EyeWidth,
                         VarietyId=b.VarietyId,
                         Gender=b.Gender,
                         RecordcottageId=b.RecordcottageId,
                         FieIdId=b.FieIdId,
                         CottageName=d.CottageName,
                         CottageTypeName=f.CottageTypeName,
                         FieIdName=e.FieIdName,
                         VarietyName= c.VarietyName,
                         CreateId=a.CreateId,
                         CreateTime=a.CreateTime,
                         UpdateId=a.UpdateId,
                         UpdateTime=a.UpdateTime,
                     }).ToList();
            if (!string.IsNullOrEmpty(request.EarNumber))
            {
                list=list.Where(x=>x.EarNumber.Contains(request.EarNumber)).ToList();
            }
            if (request.VarietyId != 0)
            {
                list=list.Where(x=>x.VarietyId==request.VarietyId).ToList();
            }
            if(request.Gender != 0)
            {
                list=list.Where(x=>x.Gender==request.Gender).ToList();
            }
            result.Data = list;
            return result;
        }
    }
}
